/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Framework;

import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.lcdui.*;
import GameExample.CMainGameScreen;
/**
 *
 * @author Wanboo.UIT
 */
public class CScreenManager implements Runnable
{
    public CGame game;
    public Vector listScreens;
    public CMyCanvas myCanvas;
    public Graphics graphics;
    public boolean paused;
    public boolean stopped;
    
    public CScreenManager(CGame _game)
    {
        game = _game;
        myCanvas = new CMyCanvas(true, this);
        graphics = this.myCanvas.getGraphics();
        stopped = true;
        listScreens = new Vector();
        
    }
    
    public void pointerPressed(int x, int y) 
    {
        synchronized (listScreens) {
            if (!this.listScreens.isEmpty()) 
            {
                CGameScreen screen = (CGameScreen) this.listScreens.lastElement();
                screen.pointerPressed(x, y);
            }
        }
    }
    
    public void pointerDragged(int x, int y) 
    {
        synchronized (listScreens) 
        {
            if (!this.listScreens.isEmpty()) {
                CGameScreen screen = (CGameScreen) this.listScreens.lastElement();
                screen.pointerDragged(x, y);
            }
        }
    }
    
    public void pointerReleased(int x, int y) 
    {
        synchronized (listScreens) 
        {
            if (!this.listScreens.isEmpty()) 
            {
                CGameScreen screen = (CGameScreen) this.listScreens.lastElement();
                screen.pointerReleased(x, y);
            }
        }
    }
    
    public void update() 
    {
        int keys = this.myCanvas.getKeyStates();

        synchronized (listScreens) 
        {
            if(listScreens.size()!= 0)
            {
                CGameScreen lastGameScreen =(CGameScreen) this.listScreens.lastElement();
                if(lastGameScreen != null)
                {                 
                    // key handler
                    lastGameScreen.keyHnd(keys);
                    // update
                    lastGameScreen.update(CTimeCounter.elapsedTime());
                   
                }
            }
        }
    }
    
    public void draw(Graphics g) 
    {
         synchronized (listScreens) 
         {
            Enumeration enumeration = listScreens.elements();
            while (enumeration.hasMoreElements()) 
            {
                CGameScreen screen = (CGameScreen) enumeration.nextElement();
                screen.draw(g);
            }
            
            myCanvas.flushGraphics();
        }
    }
    
    public void start() 
    {
        if (stopped) {
            CTimeCounter.elapsedTime();
            Thread t = new Thread(this);
            t.start();
            //Khoi tao resource
            CResourcesManager.getInst();
            //Add screen dau tien vao
            this.addScreen(new CMainGameScreen()); 
        }
    }
      
    public void run() 
    {
        this.stopped = false;
        while (!stopped) 
        {
            try {
                synchronized (this) {
                    if (paused) 
                    {
                        this.wait();
                    }
                }
                long interval = System.currentTimeMillis();
                //---------------------------------------------------------

                this.update();
                this.draw(graphics);

                //---------------------------------------------------------
                interval = System.currentTimeMillis() - interval;
                interval = ((1000 / CDefine.FPS) - interval);
                Thread.sleep(interval > 0 ? interval : 0x00);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.stopped = true;
    }
    
    public void stop() 
    {
        if (!stopped) 
            stopped = true;
    }
    
    public void resume() 
    {
        synchronized (this) 
        {
            if (paused) 
                paused = false;
            this.notify();
        }
    }
    
    public void pause() 
    {
        synchronized (this) 
        {
            if (!paused) 
            {
                paused = true;
            }
        }
    }
         
    public void addScreen(CGameScreen _screen) 
    {
        synchronized (listScreens) 
        {
            if (!listScreens.contains(_screen)) 
            {
                listScreens.addElement(_screen);
            }
        }
    }
    
    public void removeScreen(CGameScreen _screen) 
    {
        synchronized (listScreens) 
        {
            listScreens.removeElement(_screen);
            //_screen.unload();
        }
    }
    
    public Displayable getCanvas()
    {
        return this.myCanvas;
    }

}
